﻿using Microsoft.Extensions.Configuration;
using SimpleInOutControl.DBModels;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading.Tasks;

namespace SimpleInOutControl.Common
{
    /// <summary>
    /// 数据库操作
    /// </summary>
    public static class DataBaseHelper
    {
        /// <summary>
        /// 读取配置文件种连接数据库字符串
        /// </summary>
        /// <returns></returns>
        private static string GetConnectString()
        {
            var builder = new ConfigurationBuilder()
               .SetBasePath(Directory.GetCurrentDirectory())
               .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
               .AddEnvironmentVariables();

            IConfigurationRoot configuration = builder.Build();
            var connectStr = configuration.GetConnectionString("Default") ?? "";
            return connectStr;
        }

        /// <summary>
        /// 获取SqlSugarClient对象
        /// </summary>
        /// <returns></returns>
        public static SqlSugarClient GetSqlSugarClient()
        {
            string connectStr = GetConnectString();
            ConnectionConfig connectionConfig = new ConnectionConfig()
            {
                ConnectionString = connectStr,
                DbType = DbType.MySql,
                LanguageType = LanguageType.English,
                IsAutoCloseConnection = true,
            };
            SqlSugarClient client = new SqlSugarClient(connectionConfig);
            return client;
        }
    }
}
